/**
 * huanglu1 2018年4月13日
 */
package com.stars.common;

import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author huanglu1 2018年4月13日
 */
public class StringConvertUtils {

	/**
	 * 月份 String[01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12]
	 */
	public static final String[] MONTH = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };

	/**
	 * 数值正则表达式
	 */
	public static final String REGEX_NUMERICAL = "-?[0-9]+(.[0-9]+)?";

	/**
	 * 分号;
	 */
	public static final String SEMICOLON = ";";

	/**
	 * 逗号,
	 */
	public static final String COMMA = ",";

	/**
	 * 左小括号(
	 */
	public static final String PARENTHESES_LEFT = "(";

	/**
	 * 右小括号[
	 */
	public static final String PARENTHESES_RIGHT = ")";

	/**
	 * 左中括号[
	 */
	public static final String BRACKETS_LEFT = "[";

	/**
	 * 右中括号]
	 */
	public static final String BRACKETS_RIGHT = "]";

	/**
	 * 判断字符串是否为数值（整数/小数/负数）
	 * 
	 * @author huanglu1
	 * @date 2018年9月4日
	 * @param numericalStr
	 * @return
	 */
	public static boolean isNumerical(String numericalStr) {
		if (StringUtils.isBlank(numericalStr)) {
			return false;
		}
		return numericalStr.matches(REGEX_NUMERICAL);
	}

	/**
	 * 获取LIKE匹配查询条件
	 * 
	 * @author huanglu1
	 * @date 2018年9月4日
	 * @param conditionValue
	 * @return
	 */
	public static String getConditionLike(String conditionValue) {
		if (StringUtils.isBlank(conditionValue)) {
			return StringUtils.EMPTY;
		}
		String conditionLikeValue = StringUtils.replace(conditionValue, "/", "//");
		conditionLikeValue = StringUtils.replace(conditionLikeValue, "%", "/%");
		conditionLikeValue = StringUtils.replace(conditionLikeValue, "_", "/_");
		return MessageFormat.format("%{0}%", conditionLikeValue.toUpperCase());
	}

	/**
	 * 转换Set<String>为String，用;隔开
	 * 
	 * @author huanglu1 2018年4月13日
	 * @param strSet
	 * @return
	 */
	public static String convertSet2StrSemicolon(Set<String> strSet) {
		if (null == strSet || strSet.isEmpty()) {
			return null;
		}
		StringBuilder strBuilder = new StringBuilder();
		for (String userNo : strSet) {
			strBuilder.append(userNo).append(SEMICOLON);
		}
		return strBuilder.toString();
	}

	/**
	 * 截取中括号内部字符串
	 * 
	 * @author huanglu1
	 * @date 2018年7月13日
	 * @param str
	 * @return
	 */
	public static String substringBetweenBracket(String str) {
		if (StringUtils.isBlank(str)) {
			return StringUtils.EMPTY;
		}
		return StringUtils.substringBetween(str, BRACKETS_LEFT, BRACKETS_RIGHT);
	}

	/**
	 * 比较两个List的String元素是否相同
	 * 
	 * @author huanglu1
	 * @date 2018年5月2日
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static boolean compareEqualsListString(List<String> list1, List<String> list2) {
		if (null == list1 || null == list2 || list1.size() != list2.size()) {
			return false;
		}
		Set<String> set1 = new HashSet<String>();
		set1.addAll(list1);
		Set<String> set2 = new HashSet<String>();
		set2.addAll(list2);
		return compareEqualsSetString(set1, set2);
	}

	/**
	 * 比较两个Set的String元素是否相同
	 * 
	 * @author huanglu1
	 * @date 2018年5月2日
	 * @param set1
	 * @param set2
	 * @return
	 */
	public static boolean compareEqualsSetString(Set<String> set1, Set<String> set2) {
		if (null == set1 || null == set2) {
			return false;
		}
		if (set1.size() != set2.size()) {
			return false;
		}
		return set1.containsAll(set2);
	}

	public static void main(String[] args) {
		System.out.println("---------------isNumerical-----------------");
		System.out.println("  :" + StringConvertUtils.isNumerical("  "));
		System.out.println("1:" + StringConvertUtils.isNumerical("1"));
		System.out.println("0.1:" + StringConvertUtils.isNumerical("0.1"));
		System.out.println("1.0:" + StringConvertUtils.isNumerical("1.0"));
		System.out.println("-0.1:" + StringConvertUtils.isNumerical("-0.1"));
		System.out.println("--0.1:" + StringConvertUtils.isNumerical("--0.1"));

		System.out.println("---------------getConditionLike-----------------");
		System.out.println("%:" + StringConvertUtils.getConditionLike("%"));

		System.out.println("---------------convertSet2StrSemicolon-----------------");
		System.out.println(StringUtils.isNumeric("201804 "));
		System.out.println(Integer.valueOf("201804 ".trim()));
		Set<String> strSet = new HashSet<String>();
		strSet.add("1");
		strSet.add("2");
		System.out.println(convertSet2StrSemicolon(strSet));

		System.out.println("---------------substringBetweenBracket-----------------");
		Set<String> stringSet = new HashSet<>(2);
		stringSet.add("XXX");
		stringSet.add(null);
		System.out.println(substringBetweenBracket(stringSet.toString()));

		System.out.println("---------------compareEqualsListString-----------------");
		List<String> strList1 = new ArrayList<String>();
		strList1.add("1");
		strList1.add("2");
		List<String> strList2 = new ArrayList<String>();
		strList2.add("2");
		strList2.add("1");
		System.out.println(strList1.toString());
		System.out.println(strList2.toString());
		System.out.println(compareEqualsListString(strList1, strList2));

		System.out.println("---------------compareEqualsSetString-----------------");
		Set<String> strSet1 = new HashSet<String>();
		strSet1.addAll(strList1);
		Set<String> strSet2 = new HashSet<String>();
		strSet2.addAll(strList2);
		System.out.println(compareEqualsSetString(strSet1, strSet2));
	}
}
